import axios from 'axios'
import { Message } from 'element-ui'
import store from '@/store'
// create an axios instance
const service = axios.create({
  baseURL: process.env.VUE_APP_BASE_API,
  timeout: 5000
})
// 请求拦截
service.interceptors.request.use(config => {
  if (store.getters.token && !config.noToken) {
    config.headers.Authorization = 'Bearer ' + store.getters.token
  }
  return config
}, error => {
  return Promise.reject(error)
})
// 响应拦截
service.interceptors.response.use(response => {
  return response.data
}, error => {
  if (error.response && error.response.data && error.response.data.statusCode === 400) {
    Message.error(error.message)
  }

  return Promise.reject(new Error(error.message))
})
export default service
